package com.soyoung.controller;

import com.alibaba.fastjson.JSON;
import com.soyoung.entity.User;
import com.soyoung.service.UserService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;


/**
 * @author : zhq
 * @since : 2018/4/27
 * 用户测试
 */
@RequestMapping("user")
@RestController
public class UserTestController implements Serializable{

    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;

    @RequestMapping("/show")
    public String getUser(){
        List<User> user = userService.getlist();
        System.out.println(user.toString());
        return user.toString();
    }

    @RequestMapping("/show/all")
    public @ResponseBody String getUserAll(){
        List<User> user = userService.getlist();
        return JSON.toJSONString(user);
    }

    /**
     *
     * 根据id 获取用户信息
     * @param id
     * @return
     */
    @RequestMapping(value ="/show/{id}", method= RequestMethod.GET)
    public String getbyid(@PathVariable("id") Integer id){
        System.out.println("id:"+id);
        User user = userService.selectByPrimaryKey(id);
        String json = JSON.toJSONString(user);
        return json;
    }


    /**
     * 根据id 获取用户信息
     * @return
     */
    @RequestMapping(value ="/show-port", method= RequestMethod.GET)
    public String show_port(){
        int port = request.getLocalPort();
        return "当前port:"+port;
    }
}
